package com.sailorj.medical.domain.bx_record;

import java.sql.Connection;

import com.sailorj.medical.util.MyTime;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.sailorj.medical.database.DbUtil;
import com.sailorj.medical.domain.area.Area;
import com.sailorj.medical.domain.chronicdis.Chronicdis;
import com.sailorj.medical.domain.family_member.Family_member;
import com.sailorj.medical.domain.join_nh.Join_nh;
import com.sailorj.medical.domain.medical.Medical;
import com.sailorj.medical.domain.policy.Policy;
import com.sailorj.medical.exception.UserNotFoundException;

public class BX_record {
	private static final Logger LOG = LogManager.getLogger(BX_record.class);
	private String bx_record_code;// 报销记录编码
	private String user_id;// 经办人编码
	private String chronicdis_code;// 疾病编码编码
	private String id_card;// 身份证号
	private String all_cost;// 总费用
	private String bx_cost;// 报销金额
	private Timestamp vis_time;// 就诊时间
	private Timestamp bx_time;// 报销时间
	private String receipt;// 发票
	private String vis_city;// 就诊机构
	private String area_code;// 地区编号
	private int bx_state;// 报销状态(0-默认状态，未成功报销，1-已签领，报销成功)
	private String year;
	private Area area;// 地区
	private Policy policy;// 政策
	private Chronicdis chronicdis;// 疾病
	private Family_member family_member;// 家庭成员
	private Join_nh join_nh;
	private Medical medical;

	public void addBX_record(String bx_record_code, String user_id, String chronicdis_code, String id_card,
			String all_cost, String bx_cost, Timestamp vis_time, String vis_city, String area_code,
			HttpServletRequest request) throws Exception {
		Connection conn = null;
		try {
			// 初始化属性

			int id = Integer.valueOf(MyTime.getRandom(4)).intValue();
			this.bx_record_code = id_card + MyTime.getRandom(4);
			this.user_id = user_id;
			this.chronicdis_code = chronicdis_code;
			this.id_card = id_card;

			this.all_cost = all_cost;
			this.bx_cost = bx_cost;
			this.vis_time = vis_time;
			this.bx_time = MyTime.getNowForTimestamp();
			this.vis_city = vis_city;
			this.area_code = area_code;
			this.bx_state = 0;// 0-默认状态，未成功报销,等等签字确认
			this.year = MyTime.getStringNowYear();

			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
			HttpSession session = request.getSession();
			session.setAttribute("bx_record_code", this.bx_record_code);
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	/**
	 * 插入记录
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	private void saveToDB(Connection connection) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append(
					"insert into t_bx_record(bx_record_code,user_id,chronicdis_code,id_card,all_cost,bx_cost,vis_time,bx_time,vis_city,area_code,bx_state,receipt,year)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = connection.prepareStatement(sql);
			// 依次设置动态参数的值，注意顺序
			int index = 1;
			// pstmt.setInt(index++, this.id);// 行政区域编码
			pstmt.setString(index++, this.bx_record_code);// 行政区域编码
			pstmt.setString(index++, this.user_id);
			pstmt.setString(index++, this.chronicdis_code);
			pstmt.setString(index++, this.id_card);

			pstmt.setString(index++, this.all_cost);// 行政区域编码
			pstmt.setString(index++, this.bx_cost);
			pstmt.setTimestamp(index++, this.vis_time);
			pstmt.setTimestamp(index++, this.bx_time);

			pstmt.setString(index++, this.vis_city);
			pstmt.setString(index++, this.area_code);
			pstmt.setInt(index++, this.bx_state);
			pstmt.setString(index++, this.vis_city);
			pstmt.setString(index++, this.year);
			// 以executeUpdate()执行SQLbx_state
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}

	}

	public int findSum(String id_card) throws UserNotFoundException {

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int c = 0;
		try {
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("SELECT SUM(bx_cost)  FROM t_bx_record WHERE `year`="
					+ MyTime.getStringNowYear() + " AND id_card = ?");
			int index = 1;
			pstmt.setString(index++, id_card);

			// rs = pstmt.executeQuery();
			rs = pstmt.executeQuery();
			LOG.info(rs.next());
			if (rs != null) {
				c = rs.getInt(1);
				LOG.info(c);

			}
			return c;
		} catch (SQLException e) {

			throw new UserNotFoundException("User with id   could not be loaded from the database.");

		} finally {
			DbUtil.close(pstmt);
			DbUtil.close(rs, pstmt, conn);
		}
	}

	public void edit(String bx_record_code) throws SQLException {
		Connection conn = DbUtil.getConn();
		PreparedStatement pstmt = null;
		this.bx_record_code = bx_record_code;
		try {
			conn.setAutoCommit(false);

			// 定义构造update SQL语句字符串变量sql
			String sql = "update t_bx_record set bx_state=? where bx_record_code=?";
			// 创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int index = 1;
			pstmt.setString(index++, "1");
			pstmt.setString(index++, this.bx_record_code);

			int count = pstmt.executeUpdate();

			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	public BX_record() {
		super();
	}

	public BX_record(String bx_record_code, String user_id, String chronicdis_code, String id_card, String all_cost,
			String bx_cost, Timestamp vis_time, Timestamp bx_time, String receipt, String vis_city, String area_code,
			int bx_state, String year, Area area, Policy policy, Chronicdis chronicdis, Family_member family_member,
			Join_nh join_nh, Medical medical) {
		super();
		this.bx_record_code = bx_record_code;
		this.user_id = user_id;
		this.chronicdis_code = chronicdis_code;
		this.id_card = id_card;
		this.all_cost = all_cost;
		this.bx_cost = bx_cost;
		this.vis_time = vis_time;
		this.bx_time = bx_time;
		this.receipt = receipt;
		this.vis_city = vis_city;
		this.area_code = area_code;
		this.bx_state = bx_state;
		this.year = year;
		this.area = area;
		this.policy = policy;
		this.chronicdis = chronicdis;
		this.family_member = family_member;
		this.join_nh = join_nh;
		this.medical = medical;
	}

	public String getBx_record_code() {
		return bx_record_code;
	}

	public void setBx_record_code(String bx_record_code) {
		this.bx_record_code = bx_record_code;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getChronicdis_code() {
		return chronicdis_code;
	}

	public void setChronicdis_code(String chronicdis_code) {
		this.chronicdis_code = chronicdis_code;
	}

	public String getId_card() {
		return id_card;
	}

	public void setId_card(String id_card) {
		this.id_card = id_card;
	}

	public String getAll_cost() {
		return all_cost;
	}

	public void setAll_cost(String all_cost) {
		this.all_cost = all_cost;
	}

	public String getBx_cost() {
		return bx_cost;
	}

	public void setBx_cost(String bx_cost) {
		this.bx_cost = bx_cost;
	}

	public Timestamp getVis_time() {
		return vis_time;
	}

	public void setVis_time(Timestamp vis_time) {
		this.vis_time = vis_time;
	}

	public Timestamp getBx_time() {
		return bx_time;
	}

	public void setBx_time(Timestamp bx_time) {
		this.bx_time = bx_time;
	}

	public String getReceipt() {
		return receipt;
	}

	public void setReceipt(String receipt) {
		this.receipt = receipt;
	}

	public String getVis_city() {
		return vis_city;
	}

	public void setVis_city(String vis_city) {
		this.vis_city = vis_city;
	}

	public String getArea_code() {
		return area_code;
	}

	public void setArea_code(String area_code) {
		this.area_code = area_code;
	}

	public int getBx_state() {
		return bx_state;
	}

	public void setBx_state(int bx_state) {
		this.bx_state = bx_state;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public Policy getPolicy() {
		return policy;
	}

	public void setPolicy(Policy policy) {
		this.policy = policy;
	}

	public Chronicdis getChronicdis() {
		return chronicdis;
	}

	public void setChronicdis(Chronicdis chronicdis) {
		this.chronicdis = chronicdis;
	}

	public Family_member getFamily_member() {
		return family_member;
	}

	public void setFamily_member(Family_member family_member) {
		this.family_member = family_member;
	}

	public Join_nh getJoin_nh() {
		return join_nh;
	}

	public void setJoin_nh(Join_nh join_nh) {
		this.join_nh = join_nh;
	}

	public Medical getMedical() {
		return medical;
	}

	public void setMedical(Medical medical) {
		this.medical = medical;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

}
